<?php
/**
*Smarty plugin
*@package Smarty_ams
*@subpackage plugins
*
*@author:Liu Zhongjie<neojie@263.net>
*@coperation:Beijing Arouter Software Co.Ltd
*@creat Date:2007/08/01
**/

/**
* Smarty {html_pagination_ams} function plugin
*
* Type:    function<br>
* Name:    html_pagination_ams<br>
* Date:    Aug 01,2007<br>
* Input:<br>
*         -currentpage = (int)     当前页
*         -pagecount   = (int)     页数
*         -rowcount    = (int)     行数
*         -pagesize    = (int)     每页的行数
*         -msg         = (array)   文字
*
* Examples:
* <pre>
* <{html_pagination_ams font=$FONT button=$BUTTON}>
* </pre>
* @param array
* @param Smarty
* @return string
**/

function smarty_function_html_pagination_ams ($params, &$smarty)
{
	require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
	require_once $smarty->_get_plugin_filepath('function','html_options_ams');
	
	$iCurrentPage = 1;
	$iPageCount   = 1;
	$iRowCount    = 0;
	$iPageSize    = 20;
	$sSelected    = "";
	$aMsg         = null;
	$sOutput      = null;
	$aPageContent = array();
	$aTmp         = array();
	
	foreach($params as $_key => $_value)
	{
		switch($_key)
		{
			case 'currentpage':
                $iCurrentPage = (int)$_value;
                break;
            case 'rowcount':
            	$iRowCount    = (int)$_value;
            	break;
            case 'pagesize':
            	$iPageSize    = (int)$_value;
            	break;
            case 'msg':
            	$aMsg         = (array)$_value;
            	break;
            case 'pagecontent':
            	$aPageContent = (array)$_value;
            	break;
            case 'selected' :
            	$sSelected    = (string)$_value;
            	break;
            default:
            	$iCurrentPage = "";
            	$iPageCount   = "";
            	$iRowCount    = "";
            	$iPageSize    = 20;
				$aPageContent = array();
            	$aMsg         = array();
            	break;
		}
	}
	if($iCurrentPage > 1)
	{
		$sOutput.= "<a href='javascript:goPage(1);' id='pagelink'>|<<</a>"."\n";
	}
	else
	{
		$sOutput.= "|<<"."\n";
	}
	$iPageStart = $iCurrentPage;
	$iPageEnd   = $iCurrentPage;
	if("" == $iPageSize)
	{
		$iPageSize = 20;
	}
	$iPageCount = ceil($iRowCount/$iPageSize);
	for($i=1;$i <= 10;$i++)
	{
		if($iPageStart > 1)
		{
			$iPageStart--;
		}
		if($iPageEnd < $iPageCount)
		{
			$iPageEnd++;
		}
		if($iPageEnd-$iPageStart > 8)
		{
			break;
		}
	}
	for($i=$iPageStart; $i<=$iPageEnd; $i++)
	{
		if($i == $iCurrentPage)
		{
			$sOutput.= "&nbsp;".smarty_function_escape_special_chars($iCurrentPage)."&nbsp;";
		}
		elseif($i>=1 && $i<=$iPageCount)
		{
			$sOutput.= "&nbsp;"."<a href='javascript:goPage($i);' id='pagelink'>$i</a>"."&nbsp;"."\n";
		}
	}
	$sOutput.= "&nbsp;";
	if($iCurrentPage < $iPageCount)
	{
		$sOutput.= "<a href='javascript:goPage(".$iPageCount.");' id='pagelink'>>>|</a>"."\n";
	}
	else
	{
		$sOutput.= ">>|"."\n";
	}
	$sOutput.= "&nbsp;&nbsp;";
	$sOutput.= smarty_function_escape_special_chars($aMsg["recordnum"]);
	$sOutput.= smarty_function_escape_special_chars($iRowCount);
	$sOutput.= smarty_function_escape_special_chars($aMsg["pagenum"]);
	$sOutput.= smarty_function_escape_special_chars($iPageCount);
	$sOutput.= smarty_function_escape_special_chars($aMsg["page"]);
	$sOutput.= "&nbsp;&nbsp;";
	$sOutput.= smarty_function_escape_special_chars($aMsg["pagesize"]);
	$sOutput.= "<select name=\"strPageSize\" class=\"blue_select\" size=\"1\">";
	
	$aTmp['options'] = $aPageContent;
	$aTmp['selected']= $sSelected;
	
	$sOutput.= smarty_function_html_options_ams($aTmp,$smarty);
	
	
	$sOutput.= "</select>";
	$sOutput.= smarty_function_escape_special_chars($aMsg["records"]);
	
	return $sOutput;
}
?>
